package com.ikas.ai.server.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "maintain", name = "xxljob.enabled", havingValue = "true", matchIfMissing = false)
public class XxlJobConfiguration {

    @Value("${maintain.xxljob.user:admin}")
    private String userName;
    @Value("${maintain.xxljob.password:123456}")
    private String password;

    //是否开启xxljob 默认不开启
    @Value("${maintain.xxljob.enabled}")
    private Boolean enable;

    @Value("${maintain.xxljob.adminAddresses}")
    private String adminAddresses;

    @Value("${maintain.xxljob.accessToken}")
    private String accessToken;

    @Value("${maintain.xxljob.appname}")
    private String appname;

    @Value("${maintain.xxljob.ip}")
    private String ip;

    @Value("${maintain.xxljob.port}")
    private int port;

    @Value("${maintain.xxljob.logRetentionDays}")
    private int logRetentionDays;

    @Bean
    @ConditionalOnClass()
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init");
        log.info(">>>>>>>>>>> ip=" + ip + "，Port=" + port + "，address=" + adminAddresses);
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appname);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        xxlJobSpringExecutor.setIp(ip);
        return xxlJobSpringExecutor;
    }
}
